/* Copyright 2010 by Matt Van Der Westhuizen */

package org.chaoticengine.ast.view;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import org.chaoticengine.cgll.serialization.ColorConverter;
import org.newdawn.slick.Color;
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.ElementMap;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.convert.Registry;
import org.simpleframework.xml.convert.RegistryStrategy;
import org.simpleframework.xml.core.Persister;

/**
 * Used to get Images or Colors for certain model objects. The definitions
 * are loaded from a tileset file.
 *
 * @author Matt Van Der Westhuizen <mattpwest@gmail.com>
 */
@Root(name="TileSet")
public class TileFactory {
    protected static TileFactory instance = null;
    protected String filename = null;
    protected HashMap<String, Tile> tiles = new HashMap<String, Tile>();
    @Attribute(name="imgPath")
    protected String imgPath = "";
    @Attribute(name="tileHeight")
    protected int tileW = 0;
    @Attribute(name="tileWidth")
    protected int tileH = 0;

    private TileFactory() {
        filename = "";
    }

    private TileFactory(String filename) {
        this.filename = filename;
    }

    public static TileFactory getInstance() {
        if (instance == null) {
            instance = TileFactory.load("data/tilesets/default.xml");
        }

        return(instance);
    }

    public static TileFactory getInstance(String filename) {
        if (instance == null) {
            instance = TileFactory.load(filename);
        } else if (!instance.filename.equals(filename)) {
            instance = TileFactory.load(filename);
        }

        return(instance);
    }

    protected static TileFactory load(String filename) {
        Registry registry = new Registry();
        RegistryStrategy strategy = new RegistryStrategy(registry);
        try {
            registry.bind(Color.class, ColorConverter.class);
        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println("Error: Failed to register SimpleXML converter for Vector2f.");
        }
        Serializer srl = new Persister(strategy);
        File source = new File(filename);

        TileFactory result = null;
        try {
            result = srl.read(TileFactory.class, source);
            result.filename = filename;
        } catch (Exception ex) {
            result = null;
            System.out.println("Error: Failed to load TileSet from '" + filename + "'.");
            ex.printStackTrace();
        }

        return(result);
    }

    @ElementMap(entry="item", key="type", value="tile", inline=true, valueType=Tile.class)
    public HashMap<String, Tile> getTiles() {
        return(tiles);
    }

    @ElementMap(entry="item", key="type", value="tile", inline=true, valueType=Tile.class)
    public void setTiles(HashMap<String, Tile> map) {
        this.tiles = map;
    }

    public Tile getTile(String type) {
        return(tiles.get(type));
    }

    public String getImagePath() {
        return(imgPath);
    }

    public int getTileWidth() {
        return(tileW);
    }

    public int getTileHeight() {
        return(tileH);
    }

    public void loadAllImages() {
        Iterator<String> tIt = tiles.keySet().iterator();

        while (tIt.hasNext()) {
            Tile t = tiles.get(tIt.next());
            t.loadImage(imgPath);
        }
    }
}
